﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JZ_UDI.CommonHelper
{
    public static class ValidationHelper
    {
        /// <summary>
        /// 验证文本框是否为空
        /// </summary>
        /// <param name="textBoxs"></param>
        /// <returns></returns>
        public static bool CheckTextBoxIsEmpty(ErrorProvider errorProvider, params TextBox[] textBoxs)
        {
            foreach (TextBox txt in textBoxs)
            {
                if (string.IsNullOrEmpty(txt.Text) || txt.Text.Trim().Length <= 0)
                {
                    errorProvider.SetError(txt, txt.Tag.ToString());
                    return false;
                }
                else
                {
                    errorProvider.SetError(txt, string.Empty);
                    errorProvider.Clear();
                }
            }
            return true;
        }

        /// <summary>
        /// 验证文本框是否为正整数
        /// </summary>
        /// <param name="errorProvider"></param>
        /// <param name="textBoxs"></param>
        /// <returns></returns>
        public static bool CheckTextBoxIsNumber(ErrorProvider errorProvider, params TextBox[] textBoxs)
        {
            int code = 0;
            foreach (TextBox txt in textBoxs)
            {
                if (!int.TryParse(txt.Text.Trim(), out code))
                {
                    errorProvider.SetError(txt, "只能输入正整数");
                    return false;
                }
                else if (code < 0)
                {
                    errorProvider.SetError(txt, "只能输入正整数");
                    return false;
                }
                else
                {
                    errorProvider.SetError(txt, string.Empty);
                    errorProvider.Clear();
                }
            }
            return true;
        }

        /// <summary>
        /// 整数检测
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumber(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }
            return Regex.IsMatch(input, "^[0-9]+$");
        }
        /// <summary>
        /// 带符号整数检测
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumberSign(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }
            return Regex.IsMatch(input, "^[+-]?[0-9]+$");
        }

        /// <summary>
        /// 身份证检测
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsIdentity(string input)
        {
            if (string.Empty == input || "" == input) return false;
            string regIden = @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$";
            return Regex.IsMatch(input, regIden);
        }

        /// <summary>
        /// 手机验证
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static bool IsMobilePhone(string mobile)
        {
            if (string.Empty == mobile || "" == mobile) return false;
            string regMobile = @"^[1]+[3,8]+\d{9}";
            return Regex.IsMatch(mobile, regMobile);
        }

        /// <summary>
        /// 验证固定电话:0000-00000000
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static bool IsFixPhone(string phone)
        {
            if (string.Empty == phone || "" == phone) return false;
            string regPhone = @"^(\d{3,4}\-)?[1-9]\d{6,7}$";
            return Regex.IsMatch(phone, regPhone);
        }

        /// <summary>
        /// 检测是否是中文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsCH(string input)
        {
            if (string.Empty == input || "" == input) return false;
            string regCH = @"[\u4e00-\u9fa5]";
            return Regex.IsMatch(input, regCH);
        }

    }
}
